<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use app\common\model\GmEmailjob as GEJM;
use app\common\model\GmTgczlogsTqk as TQKM;

class Job extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('sendEmailAuto')
            ->addArgument('id', Argument::OPTIONAL, "your id")
            ->setDescription('the sendEmailAuto command');
    }

    protected function execute(Input $input, Output $output)
    {
        $id = trim($input->getArgument('id'));
        // 指令输出
        $output->writeln('开始执行邮件发放！执行Id='.$id);
        
        $this->logic($id);//逻辑方法  1

        $time =  date("Y-m-d H:i:s");
        $output->writeln('邮件发放任务结束！'.$time);
    }


    //逻辑代码
    public function logic($id)
    {   
        $output = $this->output; // 重新获取 $output 对象
        $row = GEJM::getSetingContent($id);
        // 假设 $row 是一个包含数据的关联数组
        if (!empty($row)) {
            // 取出相应的数据
            $data = $row[0]; // 如果只想获取第一行数据
            $id = $data['id'];
            $jobname = $data['jobname']; 
            $sendtype = $data['sendtype']; 
            $emailtitle = $data['emailtitle']; 
            $emailcontent = $data['emailcontent']; 
            $goods = $data['goods']; 
            $tou = $data['tou']; 
            $output->writeln('任务'.$jobname.'开始-----------------------------------------！id='.$id);

            $value= TQKM::SendEmailGoods($tou,$emailtitle,$emailcontent,$goods,$sendtype);

            $output->writeln('执行结果：-----------------------------------------！'.$value);

            $output->writeln('任务'.$jobname.'结束------------------------------------！id='.$id);
        } else {
            // 数据为空，可以进行相应的错误处理
        }
        

    }
}
